<?php
/**
 * Main page for showing movie list
 *
 *
 * LICENSE: Some license information
 *
 * phpMMB version: 0.0.1
 *
 * $copyright  2007 SaWey
 * $license    GNU GPL v3.
 * $URL: http://www.sawey.be/phpMyMovieBox $
 * $Revision: 6 $
 * $LastChangedBy: sander.weyens $
 * $Date: 2008-01-14 22:20:38 +0000 (Mon, 14 Jan 2008) $
*/

$tpl->define(array('main' => "main.htm"));
$tpl->define_dynamic('user_menu', 'menu');
$tpl->define_dynamic('listing_table', 'main');
$tpl->define_dynamic('listing_table_row', 'main');
$tpl->parse('MENU',".user_menu");


//get all movies + info according to the paging
$link = '?page=main';

//create sort order
if($_GET['ascdsc'] == 'd') 
{
	$ascdsc = ' DESC';
	$link .= '&ascdsc=d';
}
else
{
	$ascdsc = ' ASC';
}

if(!isset($_GET['o']) && in_array($config['show_movies_order'], $sort_orders['movies']))
{
	$order = ' ORDER BY ' . $config['show_movies_order'];
}
else if(isset($_GET['o']) && in_array($_GET['o'], $sort_orders['movies']))
{
	$order = ' ORDER BY ' . $_GET['o'];
	$link .= '&o=' . $_GET['o'];
}
else
{
	$order = ' ORDER BY movie_id';
}
$order .= $ascdsc;

//if we have a limit, create the paging
$limit = '';
$sql = 'SELECT COUNT(movie_id) FROM ' . $db_prefix . 'movies ';
$total = $db->get_var($sql);
$current_page = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0;
if($total > $config['show_movies_per_page'])
{
	//we can't show them on 1 page, create paging
	$paging = create_paging($total, $link, $current_page);
	$limit = ' LIMIT ' . $paging['limit']['start'] . ', ' . $paging['limit']['stop'];
	$tpl->assign('PAGING', $paging['nav']);
}


//select movies from db
$sql = 'SELECT movie_id FROM ' . $db_prefix . 'movies' . $order . $limit;
$movie_ids = $db->get_col($sql);

$class = 'row_1';

//build genre_icon array
$genre_icons_translated = '';
foreach($lang_genre as $english => $translated)
{
	$genre_icons_translated[$translated] = $genre_icons[$english];
}
foreach($movie_ids as $id)
{
	//get all movie data
	$movie = get_movie_info($id);

	//create genres
	$genres = '';
	if(is_array($movie['genres']))
	{
		foreach($movie['genres'] as $genre)
		{	
			if($config['genre_icons'] == 1)
			{
				$genres .= '<a href="?page=search&genre=' . $genre . '" ><img alt="' . $genre . '" border="0" src="' . $genre_icons_translated[$genre] . '" /></a> | ';
			}
			else
			{
				$genres .= '<a href="?page=search&genre=' . $genre . '" >' . $genre . '</a> | ';
			}
		}
		$genres = substr($genres, 0, (strlen($genres) - 3) );
	}
	
	
	$class = ($class == 'row_1') ? 'row_2' : 'row_1';
	//print it on the template
	$tpl->assign(array(
				'RES_TITLE'		=> $movie['main']['title'],
				'RES_POSTER'	=> $movie['main']['poster'],
				'RES_MOVIE_ID'	=> $movie['main']['movie_id'],
				'RES_GENRE'		=> $genres,
				'RES_RATING'	=> $movie['main']['imdb_rating'] . "/10",
				'ALTERNATE_COLORS_CLASS' => $class,
				));
	$tpl->parse('LISTING_TABLE_ROW', '.listing_table_row');
}

$tpl->parse('BODY', '.listing_table');
?>